package com.game;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class DBAdapter {
	public static final String DATABASE_NAME = "gameList.db";
	public static final String DATABASE_TABLE_GAME_ITEM = "gameItems";
	public static final String DATABASE_TABLE_STEP_ITEM = "stepItems";
	public static final String DATABASE_TABLE_GAME = "game";

	private static final int DATABASE_VERSION = 4;

	public static final String _ID = "_id";
	public static final String GAME_ID = "game_id";
	public static final String CHOICE_NAME = "choice_name";
	public static final String TEXT_PRE = "text_pre";
	public static final String TEXT_POST = "text_post";
	public static final String CHOICE_FROM = "choice_from";
	public static final String CHOICE_TO = "choice_to";
	public static final String GAME_NAME = "game_name";
	public static final String GAME_DESC = "game_desc";
	public static final String GAME_INSTR = "game_instr";

	private static final String [] FROM_GAME_ITEM= { _ID, GAME_ID, CHOICE_NAME,
			TEXT_PRE, TEXT_POST };

	private static final String [] FROM_LIST_GAME_ITEM= { _ID, GAME_ID, CHOICE_NAME};
	
	private static final String [] FROM_STEP_ITEM= { _ID, GAME_ID, CHOICE_FROM,CHOICE_TO};
	
	private static final String [] FROM_GAME= { _ID, GAME_NAME, GAME_DESC,GAME_INSTR };

	private static final String FROM_LIST_GAME[] = { GAME_NAME };

	private SQLiteDatabase db;
	private final Context context;
	private DBOpenHelper dbHelper;

	public DBAdapter(Context _context) {
		this.context = _context;
		dbHelper = new DBOpenHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION);

	}

	private static class DBOpenHelper extends SQLiteOpenHelper {
		public DBOpenHelper(Context context, String name,
				CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		private static final String DATABASE_CREATE_GAME_ITEM = "create table "
				+ DATABASE_TABLE_GAME_ITEM + " (" + _ID
				+ " integer primary key autoincrement, " + GAME_ID
				+ " integer, " + CHOICE_NAME + " text not null, " + TEXT_PRE
				+ " text not null, " + TEXT_POST + " text not null);";

		private static final String DATABASE_CREATE_STEP_ITEM = "create table "
				+ DATABASE_TABLE_STEP_ITEM + " (" + _ID
				+ " integer primary key autoincrement, " + GAME_ID
				+ " integer, " + CHOICE_FROM + " text not null, " + CHOICE_TO
				+ " text not null);";

		private static final String DATABASE_CREATE_GAME = "create table "
				+ DATABASE_TABLE_GAME + " (" + _ID
				+ " integer primary key autoincrement, " + GAME_NAME
				+ " text not null, " + GAME_DESC + " text not null, "
				+ GAME_INSTR + " text not null);";

		public void onCreate(SQLiteDatabase _db) {
			_db.execSQL(DATABASE_CREATE_GAME_ITEM);
			_db.execSQL(DATABASE_CREATE_STEP_ITEM);
			_db.execSQL(DATABASE_CREATE_GAME);

		}

		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
				int _newVersion) {
			Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion
					+ " to " + _newVersion
					+ ", which will destroy all old data");

			// Drop the old table.
			_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_GAME_ITEM);
			_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_STEP_ITEM);
			_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_GAME);

			// Create a new one.
			onCreate(_db);
		}

	}

	public void close() {
		db.close();
	}

	public void open() throws SQLiteException {
		try {
			db = dbHelper.getWritableDatabase();
		} catch (SQLiteException ex) {
			db = dbHelper.getReadableDatabase();
		}
	}

	public Cursor getAllGameItemsCursor() {
		return db.query(DATABASE_TABLE_GAME_ITEM, FROM_GAME_ITEM, null, null,
				null, null, null);

	}

	public Cursor getAllStepItemsCursor() {
		return db.query(DATABASE_TABLE_STEP_ITEM, FROM_STEP_ITEM, null, null,
				null, null, null);

	}

	public Cursor getAllGameCursor() {
		return db.query(DATABASE_TABLE_GAME, FROM_GAME, null, null, null, null,
				null);

	}


	public Cursor getAllItemCursor(String tableName){
		if (tableName.equals(DATABASE_TABLE_GAME_ITEM)) {
			return db.query(DATABASE_TABLE_GAME_ITEM, FROM_GAME_ITEM, null, null,
					null, null, null);
		} else if (tableName.equals(DATABASE_TABLE_STEP_ITEM)) {
			return db.query(DATABASE_TABLE_STEP_ITEM, FROM_STEP_ITEM, null, null,
					null, null, null);
		} else if (tableName.equals(DATABASE_TABLE_GAME)) {
			return db.query(DATABASE_TABLE_GAME, FROM_GAME, null, null, null, null,
					null);
		} else {
			return null;
		}
	}
	
	public Cursor getGameByNameCursor(String gameName) {
		return db.query(DATABASE_TABLE_GAME, new String[] { _ID, GAME_DESC,
				GAME_INSTR }, GAME_NAME + "=" + "\"" + gameName + "\"", null,
				null, null, null);
	}

	public Cursor getGameByIdCursor(int _Id) {
		return db.query(DATABASE_TABLE_GAME, FROM_GAME, _ID + "=" + Integer.toString(_Id), null,
				null, null, null);
	}
	
	public Cursor getGameItemByIdCursor(int _Id) {
		return db.query(DATABASE_TABLE_GAME_ITEM, FROM_GAME_ITEM, _ID + "=" + Integer.toString(_Id), null,
				null, null, null);
	}
	
	public Cursor getStepItemByIdCursor(int _Id) {
		return db.query(DATABASE_TABLE_STEP_ITEM, FROM_STEP_ITEM, _ID + "=" + Integer.toString(_Id), null,
				null, null, null);
	}
	
	
	
	public Cursor getNextStepsCursor(int gameId, String stepName) {
		return db.query(DATABASE_TABLE_STEP_ITEM, new String[] { CHOICE_TO },
				CHOICE_FROM + "=" + "\"" + stepName + "\"", null, null, null,
				null);
	}
	
	public Cursor getNextStepsCursorRaw(int gameId, String stepName) {
		String [] args = {stepName, Integer.toString(gameId)};
		return db.rawQuery("select choice_name, text_pre from game_item join step_item on choice_to = choice_name where choice_from =? and game_id = ?", args);
	}
	
	
	
	public Cursor getFirstStepsCursor(int gameId, String stepName) {
		return db.query(DATABASE_TABLE_STEP_ITEM, new String[] { CHOICE_TO },
				CHOICE_FROM + "=" + "\"" + "start" + "\"", null, null, null,
				null);
	}

	public Cursor getFirstStepsCursorRaw(int gameId) {
		String [] args = {Integer.toString(gameId)};
		return db.rawQuery("select choice_name, text_pre from game_item join step_item on choice_to = choice_name where choice_from = \"start\" and game_id = ?", args);
	}

	
	
	public long insertStepItemData(StepItem _step) {

		// Create a new row of values to insert.
		ContentValues newTaskValues = new ContentValues();
		newTaskValues.put(GAME_ID, _step.getGameId());
		newTaskValues.put(CHOICE_FROM, _step.getChoiceFrom());
		newTaskValues.put(CHOICE_TO, _step.getChoiceTo());

		// Insert the row.
		return db.insertOrThrow(DATABASE_TABLE_STEP_ITEM, null, newTaskValues);
		// return a;
	}

	public long insertGameItemData(GameItem _gameItem) {

		// Create a new row of values to insert.
		ContentValues newTaskValues = new ContentValues();
		newTaskValues.put(GAME_ID, _gameItem.getGameId());
		newTaskValues.put(CHOICE_NAME, _gameItem.getChoiceName());
		newTaskValues.put(TEXT_PRE, _gameItem.getTextPre());
		newTaskValues.put(TEXT_POST, _gameItem.getTextPost());

		// Insert the row.
		return db.insertOrThrow(DATABASE_TABLE_GAME_ITEM, null, newTaskValues);
		// return a;
	}

	public long insertGameData(Game _game) {

		// Create a new row of values to insert.
		ContentValues newTaskValues = new ContentValues();
		newTaskValues.put(GAME_NAME, _game.getGameName());
		newTaskValues.put(GAME_DESC, _game.getGameDesc());
		newTaskValues.put(GAME_INSTR, _game.getGameInstr());

		// Insert the row.
		return db.insertOrThrow(DATABASE_TABLE_GAME, null, newTaskValues);
		// return a;
	}

	public boolean removeGameItem(int _id) {
		return db.delete(DATABASE_TABLE_GAME_ITEM, _ID + "=" + _id, null) > 0;
	}

	public boolean removeStepItem(int _id) {
		return db.delete(DATABASE_TABLE_STEP_ITEM, _ID + "=" + _id, null) > 0;
	}

	public boolean removeGame(int _id) {
		return db.delete(DATABASE_TABLE_GAME, _ID + "=" + _id, null) > 0;
	}

	public boolean removeItem(String tableName,int _id) {
		return db.delete(tableName, _ID + "=" + _id, null) > 0;
	}
	
	public boolean removeItem(int _id,String tableName){
		return db.delete(tableName, _ID + "=" + _id, null) > 0;
		
	}
	
	public String[] getFromGameItem() {
		return FROM_GAME_ITEM;
	}


	public String[] getFromStepItem() {
		return FROM_STEP_ITEM;
	}


	public String[] getFromGame() {
		return FROM_GAME;
	}


	public String[] getFromGameName() {
		return FROM_LIST_GAME;
	}

	
	public String[] getFrom(String tableName) {
		if (tableName.equals(DATABASE_TABLE_GAME_ITEM)) {
			return FROM_GAME_ITEM;
		} else if (tableName.equals(DATABASE_TABLE_STEP_ITEM)) {
			return FROM_STEP_ITEM;
		} else if (tableName.equals(DATABASE_TABLE_GAME)) {
			return FROM_GAME;
		} else {
			return null;
		}
	}
	
	public String[] getFromList(String tableName) {
		if (tableName.equals(DATABASE_TABLE_GAME_ITEM)) {
			return FROM_LIST_GAME_ITEM;
		} else if (tableName.equals(DATABASE_TABLE_STEP_ITEM)) {
			return FROM_STEP_ITEM;
		} else if (tableName.equals(DATABASE_TABLE_GAME)) {
			return FROM_LIST_GAME;
		} else {
			return null;
		}
	}
	

	

}
